package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Curso;


public class CursoDAO implements ICursoDAO {
    private static final Log log = LogFactory.getLog(CursoDAO.class);

    // property constants
    //public static final String  CURSDESCRIPV = "cursdescripv";
    public static final String CURSDESCRIPV = "cursdescripv";

    //public static final Integer  CURSIDN = "cursidn";
    public static final String CURSIDN = "cursidn";

    //public static final String  CURSNOMBREV = "cursnombrev";
    public static final String CURSNOMBREV = "cursnombrev";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Curso Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Curso instance) {
        log.debug("saving Curso instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Curso Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Curso instance) {
        log.debug("deleting Curso instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Curso Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Curso instance) {
        log.debug("updating Curso instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Curso findById(Integer id) {
        log.debug("finding Curso instance with id: " + id);

        try {
            Curso instance = (Curso) getSession().get(Curso.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Curso failed", re);
            throw re;
        }
    }

    public List<Curso> findByExample(Curso instance) {
        log.debug("finding Curso instance by example");

        try {
            List results = getSession().createCriteria("Curso")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Curso entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Curso> found by query
        */
    public List<Curso> findByProperty(String propertyName, Object value) {
        log.debug("finding Curso instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Curso as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Curso> findByCursdescripv(Object cursdescripv) {
        return findByProperty(CURSDESCRIPV, cursdescripv);
    }

    public List<Curso> findByCursidn(Object cursidn) {
        return findByProperty(CURSIDN, cursidn);
    }

    public List<Curso> findByCursnombrev(Object cursnombrev) {
        return findByProperty(CURSNOMBREV, cursnombrev);
    }

    /**
    * Find all Curso entities.
    *
    * @return List<Curso> all Curso instances
    */
    public List<Curso> findAll() {
        log.debug("finding all Curso instances");

        try {
            String queryString = "from Curso";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Curso> findByCriteria(String whereCondition) {
        log.debug("finding Curso " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Curso model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Curso> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Curso failed", re);
            throw re;
        }
    }

    public List<Curso> findPageCurso(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Curso findPageCurso");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Curso model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Curso model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberCurso() {
        log.debug("finding Curso count");

        try {
            String queryString = "select count(*) from Curso model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    
    public List<Curso> findAllGrupo() {
        log.debug("finding all Curso instances");

        try {
            String queryString = "from Curso where cur_cursidn is null";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
    
    
 
    public List<Curso> findAllSalon(Integer cursidn) {
        log.debug("finding all Salon instances");

        try {
            String queryString = "from Curso where cur_cursidn="+cursidn+ "order by cursdescripv asc";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
    
    
}
